﻿using System;
using System.ComponentModel;

namespace DotNetCommon.Serialize
{
    /// <summary>
    /// 专为 .net6 提供
    /// </summary>
    public sealed class JsonConverterDateOnly : System.Text.Json.Serialization.JsonConverter<DateOnly>
    {
        public string Format { get; set; }
        public JsonConverterDateOnly()
        {
            this.Format = "yyyy-MM-dd";
        }
        public JsonConverterDateOnly(string format)
        {
            if (format == null) throw new ArgumentNullException("format");
            Format = format;
        }
        public override DateOnly Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
        {
            return DateOnly.Parse(reader.GetString());
        }

        public override void Write(System.Text.Json.Utf8JsonWriter writer, DateOnly value, System.Text.Json.JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(Format));
        }
    }

    /// <summary>
    /// 专为 .net6 提供
    /// </summary>
    public sealed class JsonConverterTimeOnly : System.Text.Json.Serialization.JsonConverter<TimeOnly>
    {
        public string Format { get; set; }
        public JsonConverterTimeOnly()
        {
            this.Format = "HH:mm:ss.fffffff";
        }
        public JsonConverterTimeOnly(string format)
        {
            if (format == null) throw new ArgumentNullException("format");
            Format = format;
        }
        public override TimeOnly Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
        {
            return TimeOnly.Parse(reader.GetString());
        }

        public override void Write(System.Text.Json.Utf8JsonWriter writer, TimeOnly value, System.Text.Json.JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(Format));

        }
    }

    public class JsonConverterDatetime : System.Text.Json.Serialization.JsonConverter<DateTime>
    {
        public string Format { get; set; }
        public JsonConverterDatetime(string format)
        {
            if (format == null) throw new ArgumentNullException("format");
            Format = format;
        }
        public override DateTime Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
        {
            return DateTime.Parse(reader.GetString());
        }

        public override void Write(System.Text.Json.Utf8JsonWriter writer, DateTime value, System.Text.Json.JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(Format));
        }
    }

    public class JsonConverterDateTimeOffset : System.Text.Json.Serialization.JsonConverter<DateTimeOffset>
    {
        public string Format { get; set; }
        public JsonConverterDateTimeOffset(string format)
        {
            if (format == null) throw new ArgumentNullException("format");
            Format = format;
        }
        public override DateTimeOffset Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
        {
            return DateTimeOffset.Parse(reader.GetString());
        }

        public override void Write(System.Text.Json.Utf8JsonWriter writer, DateTimeOffset value, System.Text.Json.JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(Format));
        }
    }

}
